<template>
  <!-- @close="handleClose":关闭弹框定义的方法 -->
  <el-dialog
    title="新增员工"
    :visible="showDialog"
    @close="handleClose"
    @open="handleOpen"
    top="8vh"
  >
    <!-- 表单 -->
    <el-form
      label-width="120px"
      ref="addForm"
      :model="employeeForm"
      :rules="rules"
    >
      <el-form-item label="姓名" prop="username">
        <el-input
          style="width: 80%"
          placeholder="请输入姓名"
          v-model="employeeForm.username"
        />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input
          style="width: 80%"
          placeholder="请输入手机号"
          v-model="employeeForm.mobile"
        />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          style="width: 80%"
          placeholder="请选择入职时间"
          v-model="employeeForm.timeOfEntry"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select
          v-model="employeeForm.formatEmployment"
          style="width: 80%"
          placeholder="请选择"
        >
          <!-- :value="item.id"：给后台，:label="item.value"：给用户 -->
          <el-option
            v-for="item in hireTypes"
            :key="item.id"
            :value="item.id"
            :label="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input
          v-model="employeeForm.workNumber"
          style="width: 80%"
          placeholder="请输入工号"
        />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <!--  readonly：只读（不会出现灰色）
              disabled：禁用（会出现灰色）
         -->
        <el-input
          v-model="employeeForm.departmentName"
          readonly
          style="width: 80%"
          placeholder="请选择部门"
          @focus="isShow = true"
        />
        <!-- @focus="isShow = true"
          @blur="isShow = false"这是新增员工的部门事件的弹框显示与隐藏 -->
        <!-- 部门树形组件 :data="deptTreeData" v-if="isShow"：控制弹框的显示与隐藏-->
        <el-tree
          v-if="isShow"
          :data="deptTreeData"
          :props="{ label: 'name' }"
          @node-click="handleNodeClick"
        />
      </el-form-item>
      <!-- value-format="yyyy-MM-dd"这个是把时间转化为字符串 见emelent组价  -->
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker
          style="width: 80%"
          placeholder="请选择转正时间"
          v-model="employeeForm.correctionTime"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleAdd">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
//导入员工常量模块
import obj from "@/constant/employees.js";
import { getDeptListAPI } from "@/api/departments";
import { addEmployeeAPI } from "@/api/employees";
import { list2Tree } from "@/utils/shift"; //获取树形结构的内容
export default {
  name: "add-employee",
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      //聘用形式列表 搞个对象
      hireTypes: obj.hireType,
      //部门树形数据
      deptTreeData: [],
      //控制树形组件的显示与隐藏
      isShow: false,
      //员工表单
      employeeForm: {
        uesrname: "",
        mobile: "",
        formOfEmployment: 1,
        workNumber: "",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: "",
      },
      //新增校验
      rules: {
        username: [
          {
            //姓名
            required: true,
            message: "用户姓名不能为空",
            trigger: ["blur"],
          },
          {
            min: 1,
            max: 10,
            message: "用户姓名为1-10位",
            trigger: ["blur"],
          },
        ],
        mobile: [
          {
            //手机
            required: true,
            message: "手机号不能为空",
            trigger: ["blur"],
          },
          {
            pattern:
              /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/,
            message: "手机号格式不正确",
            trigger: ["blur"],
          },
        ],
        formOfEmployment: [
          {
            //聘用形式
            required: true,
            message: "聘用形式不能为空",
            trigger: ["blur"],
          },
        ],
        workNumber: [
          {
            //工号
            required: true,
            message: "工号不能为空",
            trigger: ["blur"],
          },
        ],
        departmentName: [
          {
            //部门
            required: true,
            message: "部门不能为空",
            trigger: ["change"],
          },
        ],
        timeOfEntry: [
          {
            //入职时间
            required: true,
            message: "请选择入职时间",
            trigger: ["blur"],
          },
        ],
        correctionTime: [
          {
            //转正时间
            required: true,
            message: "转正时间不能为空",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods: {
    //弹框关闭时子传父
    handleClose() {
      this.$emit("update:showDialog", false);
      //重置表单
      this.$refs.addForm.resetFields();
    },
    //弹框打开时
    async handleOpen() {
      //发起部门列表请求
      const resp = await getDeptListAPI();
      //转化为树形结构，在进行赋值
      this.deptTreeData = list2Tree(resp.data.depts, "");
    },
    //树形节点点击时
    handleNodeClick(node) {
      //node：每一行被点击的节点对象
      if (node.children) return;
      // 否则否则当前选择的部门
      this.employeeForm.departmentName = node.name;
      //关闭部门树形组件隐藏
      this.isShow = false;
    },
    //新增员工 （需要进行封装接口 创建，导入，使用）
    handleAdd() {
      //整体校验
      this.$refs.addForm.validate(async (valid) => {
        // 如果校验不通过，直接结束执行
        if (!valid) return;
        //发起新增请求
        await addEmployeeAPI(this.employeeForm);
        //关闭弹框
        this.handleClose();
        //给个成功的提示
        this.$message.success("添加成功");
        //获取员工列表
        //方式1：$parent(不推荐，不安全)
        // this.$parent.getEmployeeList()不推荐用父组件
        // 方式2.推荐子传父（$emit）
        this.$emit("add-success");
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.el-tree {
  position: absolute; //绝对定位会脱离文档流
  width: 80%;
  min-height: 50px;
  left: 0;
  top: 45px;
  z-index: 100;
  border: 1px solid #ccc;
  border-radius: 4px;
  padding-right: 5px;
  overflow: hidden;
  background-color: #fff;
  max-height: 200px;
  overflow: auto;
  ::v-deep {
    //要想进行穿透需要加上scoped
    .el-tree-node__content {
      height: auto;
    }
  }
}
</style>
